package com.chebaohui.iqs.service.impl.deal.bihu.profess;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.BiHuConfig;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.InsuranceCode;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.base.CarOrder;
import com.chebaohui.iqs.entity.base.Company;
import com.chebaohui.iqs.entity.base.Insurance;
import com.chebaohui.iqs.entity.req.CarPrice;
import com.chebaohui.iqs.entity.res.CompanyCarPrice;
import com.chebaohui.iqs.service.impl.deal.bihu.entity.req.CarQuotation;
import com.chebaohui.iqs.service.impl.deal.bihu.entity.req.CarQuotationGet;
import com.chebaohui.iqs.service.impl.deal.bihu.entity.req.GetreInfo;
import com.chebaohui.iqs.service.impl.deal.bihu.num.OneInterface;
import com.chebaohui.iqs.service.impl.deal.bihu.num.ThreeInterface;
import com.chebaohui.iqs.service.impl.deal.bihu.num.TwoInterface;
import com.chebaohui.iqs.service.impl.deal.bihu.utils.EnumUtil;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;
import com.chebaohui.iqs.service.impl.deal.util.TimeBase;

/**
 * 对外输出<针对车保汇后台>--车险算费
 * 
 * @author xielonghai
 *
 */
public class BiHuCarPrice {
	/**
	 * log:日志输出
	 */
	private Log log = LogFactory.getLog(BiHuCarPrice.class);

	private Company company;// 所需返回保险公司编码

	private CarPrice param;// 报价参数

	private CompanyCarPrice result;// 返回结果

	private GetreInfo bihuOne;// 壁虎接口1提交参数

	private CarQuotation bihuTwo;// 壁虎接口2提交参数

	private CarQuotationGet bihuThree;// 壁虎接口3提交参数

	private String busBeginTime;// 商业险起保时间

	private String traBeginTime;// 交强险起保时间

	private int group;// 意向算费公司（单个的枚举值）

	private String custzkey;

	/**
	 * 构造器
	 * 
	 * @param param
	 * @param mapCode
	 */
	public BiHuCarPrice(CarPrice param, Company company) {
		this.custzkey = BiHuConfig.getCustKey();// MD5Util.getMD5(param.getOwner().getPhone())
		this.param = param;
		this.company = company;
		this.bihuTwo = new CarQuotation();
		this.bihuThree = new CarQuotationGet();
		this.bihuOne = new GetreInfo();
		this.result = new CompanyCarPrice();
		result.setCompanyCode(company.getCompanyCode());
		result.setModelCode(company.getModelCode());
	}

	/**
	 * 报价<接口1（获取保险起保时间）--->接口2信息注入--->接口3报价获取 >
	 * 
	 * @return
	 */
	public CompanyCarPrice price() {
		try {
			checkOneIter();

			setInsuranceOne();

			setResult(priceGo());

			result.setCode(ResultCode.SUCCESS.getCode());
			result.setMsg(ResultCode.SUCCESS.getMsg());
			return result;
		} catch (Exception e) {
			if (!StringUtil.isNulls(e.getMessage()) && e.getMessage().indexOf("重复投保:") == 0) {
				result.setCode(ResultCode.N04.getCode());
				result.setMsg(ResultCode.N04.getMsg() + e.getMessage().substring(e.getMessage().indexOf("重复投保:") + 5));
			} else {
				result.setCode(ResultCode.N02.getCode());
				result.setMsg(ResultCode.N02.getMsg() + e.getMessage());
			}
			return result;
		}
	}

	private final int repeatMax = 3;// 重复进行接口2-3的最大次数----->与处理逻辑有关
	private int repeatNum = 1;// 报价不走记录

	/**
	 * 报价步骤:2--3
	 * 
	 * @return
	 * @throws Exception
	 */
	private JSONObject priceGo() throws Exception {
		JSONObject twoRes = new TwoInterface().two(bihuTwo);

		if (null == twoRes || twoRes.isEmpty())
			throw new Exception("壁虎接口2返回空");

		if (!"1".equals(twoRes.getString("BusinessStatus").trim()))
			throw new Exception("壁虎2接口请求失败:" + twoRes.getString("StatusMessage"));

		setThreeBiHu();
		
		log.info("保险公司："+bihuThree.getQuoteGroup());
		
		JSONObject threeRes = new ThreeInterface().three(bihuThree);

		if (null == threeRes || threeRes.isEmpty())
			throw new Exception("壁虎接口3返回空");

		if (!"1".equals(threeRes.getString("BusinessStatus").trim()))
			throw new Exception("壁虎3接口请求失败:" + threeRes.getString("StatusMessage"));

		if (!repeatDeal(threeRes))
			return threeRes;
		else {
			if (repeatNum > repeatMax)// 大于3次重复进行报价---》异常
				throw new Exception("壁虎报价循环出现错误");
			else
				repeatNum++;

			return priceGo();
		}
	}

	/**
	 * 重复投保处理
	 */
	private boolean repeatDeal(JSONObject res) throws Exception {
		String QuoteResult = res.getJSONObject("Item").getString("QuoteResult");
		log.info("壁虎接口3获取报价状态信息:" + QuoteResult);
		int QuoteStatus = res.getJSONObject("Item").getIntValue("QuoteStatus");
		if (QuoteStatus < 0)
			throw new Exception("壁虎未进行报价");
		else {
			if (QuoteResult.indexOf("重复投保") >= 0) {// 重复投保
				if (setTime(res))
					return true;
				else {
					throw new Exception("重复投保:" + QuoteResult);
				}
			} else {
				if (QuoteStatus > 0)
					return false;
				else{
					log.error("壁虎报价返回错误:" + QuoteResult );
					throw new Exception(QuoteResult);
			}
			}
		}
	}

	/**
	 * 保险起保时间处理判断
	 * 
	 * @param res
	 * @return
	 * @throws Exception
	 */
	private boolean setTime(JSONObject res) throws Exception {
		boolean bus = false, tra = false;
		/** 交强险 */
		String ForceExpireDate = res.getJSONObject("UserInfo").getString("ForceExpireDate");
		if (!StringUtil.isNulls(ForceExpireDate)) {
			if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(traBeginTime)
					.getTime() < new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(ForceExpireDate).getTime()) {
				traBeginTime = ForceExpireDate;
				tra = true;
				long times = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(traBeginTime).getTime() / 1000;
				bihuTwo.setForceTimeStamp(times + "");
			}
		}

		/** 商业险 **/
		String BusinessExpireDate = res.getJSONObject("UserInfo").getString("BusinessExpireDate");
		if (!StringUtil.isNulls(BusinessExpireDate)) {
			if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(busBeginTime)
					.getTime() < new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(BusinessExpireDate).getTime()) {
				busBeginTime = BusinessExpireDate;
				bus = true;
				long times = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(busBeginTime).getTime() / 1000;
				bihuTwo.setBizTimeStamp(times + "");
			}
		}

		return (bus || tra);
	}

	/**
	 * 设置返回结果
	 */
	private void setResult(JSONObject res) throws Exception {
		CarOrder order = new CarOrder();
		order.setOrederNo(bihuThree.getCustKey());
		BigDecimal total = new BigDecimal("0.0");
		// 判断商业险是否在90天内
		boolean bus = (!StringUtil.isNulls(res.getJSONObject("Item").getString("BizTotal"))
				&& res.getJSONObject("Item").getDoubleValue("BizTotal") > 0);
		if (bus) {
			order.setBusNo(bihuThree.getCustKey());
			order.setBusBeginTime(res.getJSONObject("UserInfo").getString("BusinessStartDate"));
			order.setBusEndTime(TimeBase.getNextYear(res.getJSONObject("UserInfo").getString("BusinessStartDate"),
					TimeBase.time_default));
			order.setBusTotalPrice(res.getJSONObject("Item").getString("BizTotal"));
			total = total.add(new BigDecimal(res.getJSONObject("Item").getString("BizTotal")));
		}
		// 判断交强险是否在90天内
		boolean tra = (!StringUtil.isNulls(res.getJSONObject("Item").getString("ForceTotal"))
				&& res.getJSONObject("Item").getDoubleValue("ForceTotal") > 0);
		if (tra) {
			order.setTraNo(bihuThree.getCustKey());
			order.setTraPrice(res.getJSONObject("Item").getString("ForceTotal"));
			total = total.add(new BigDecimal(res.getJSONObject("Item").getString("ForceTotal")));
			order.setTaxPrice(res.getJSONObject("Item").getString("TaxTotal"));
			total = total.add(new BigDecimal(res.getJSONObject("Item").getString("TaxTotal")));
			order.setTraBeginTime(res.getJSONObject("UserInfo").getString("ForceStartDate"));
			order.setTraEndTime(TimeBase.getNextYear(res.getJSONObject("UserInfo").getString("ForceStartDate"),
					TimeBase.time_default));
		}

		order.setTotalCarPrice(total.toString());

		List<Insurance> insuranceList = new ArrayList<Insurance>();
		JSONObject Item = res.getJSONObject("Item");

		if (tra) {// 交强险
			Insurance tp = new Insurance();
			tp.setCode(InsuranceCode.TP.getCode());
			tp.setIndemnity("false");
			tp.setInsurancePrice(order.getTraPrice());
			insuranceList.add(tp);
		}

		if (Item.getJSONObject("CheSun").getDouble("BaoFei") > 0) {// 车损险
			// 重新设置车损的保费和保额
			Insurance bl = new Insurance();
			bl.setCode(InsuranceCode.BL.getCode());
			bl.setAuount(Item.getJSONObject("CheSun").getString("BaoE"));
			bl.setInsurancePrice(Item.getJSONObject("CheSun").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianCheSun").getDouble("BaoFei") > 0) {
				bl.setIndemnityPrice(Item.getJSONObject("BuJiMianCheSun").getString("BaoFei"));
				bl.setIndemnity("true");
			} else
				bl.setIndemnity("false");
			insuranceList.add(bl);
		}

		if (Item.getJSONObject("DaoQiang").getDouble("BaoFei") > 0) {// 盗抢险
			Insurance bt = new Insurance();
			bt.setCode(InsuranceCode.BT.getCode());
			bt.setAuount(Item.getJSONObject("DaoQiang").getString("BaoE"));
			bt.setInsurancePrice(Item.getJSONObject("DaoQiang").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianDaoQiang").getDouble("BaoFei") > 0) {
				bt.setIndemnityPrice(Item.getJSONObject("BuJiMianDaoQiang").getString("BaoFei"));
				bt.setIndemnity("true");
			} else
				bt.setIndemnity("false");
			insuranceList.add(bt);
		}

		if (Item.getJSONObject("SanZhe").getDouble("BaoFei") > 0) {// 三者险
			Insurance btr = new Insurance();
			btr.setCode(InsuranceCode.BTR.getCode());
			btr.setAuount(Item.getJSONObject("SanZhe").getString("BaoE"));
			btr.setInsurancePrice(Item.getJSONObject("SanZhe").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianSanZhe").getDouble("BaoFei") > 0) {
				btr.setIndemnityPrice(Item.getJSONObject("BuJiMianSanZhe").getString("BaoFei"));
				btr.setIndemnity("true");
			} else
				btr.setIndemnity("false");
			insuranceList.add(btr);
		}

		if (Item.getJSONObject("SiJi").getDouble("BaoFei") > 0) {// 司机险
			Insurance sj = new Insurance();
			sj.setCode(InsuranceCode.BD.getCode());
			sj.setAuount(Item.getJSONObject("SiJi").getString("BaoE"));
			sj.setInsurancePrice(Item.getJSONObject("SiJi").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianSiJi").getDouble("BaoFei") > 0) {
				sj.setIndemnityPrice(Item.getJSONObject("BuJiMianSiJi").getString("BaoFei"));
				sj.setIndemnity("true");
			} else
				sj.setIndemnity("false");
			insuranceList.add(sj);
		}

		if (Item.getJSONObject("ChengKe").getDouble("BaoFei") > 0) {// 乘客险
			Insurance bp = new Insurance();
			bp.setCode(InsuranceCode.BP.getCode());
			bp.setAuount(Item.getJSONObject("ChengKe").getString("BaoE"));
			bp.setInsurancePrice(Item.getJSONObject("ChengKe").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianChengKe").getDouble("BaoFei") > 0) {
				bp.setIndemnityPrice(Item.getJSONObject("BuJiMianChengKe").getString("BaoFei"));
				bp.setIndemnity("true");
			} else
				bp.setIndemnity("false");
			insuranceList.add(bp);
		}

		if (Item.getJSONObject("HuaHen").getDouble("BaoFei") > 0) {// 车身划痕损失险
			Insurance bn = new Insurance();
			bn.setCode(InsuranceCode.BN.getCode());
			bn.setAuount(Item.getJSONObject("HuaHen").getString("BaoE"));
			bn.setInsurancePrice(Item.getJSONObject("HuaHen").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianHuaHen").getDouble("BaoFei") > 0) {
				bn.setIndemnityPrice(Item.getJSONObject("BuJiMianHuaHen").getString("BaoFei"));
				bn.setIndemnity("true");
			} else
				bn.setIndemnity("false");
			insuranceList.add(bn);
		}

		if (Item.getJSONObject("BoLi").getDouble("BaoFei") > 0) {// 玻璃险
			Insurance bg = new Insurance();
			bg.setCode(InsuranceCode.BG.getCode());
			bg.setAuount(Item.getJSONObject("BoLi").getString("BaoE"));
			bg.setInsurancePrice(Item.getJSONObject("BoLi").getString("BaoFei"));
			bg.setType(bihuTwo.getBoLi() == 2.0 ? "01" : "00");
			insuranceList.add(bg);
		}

		if (Item.getJSONObject("HcXiuLiChang").getDouble("BaoFei") > 0) {// 指定修理险
			Insurance br = new Insurance();
			br.setCode(InsuranceCode.BR.getCode());
			br.setAuount(Item.getJSONObject("HcXiuLiChang").getString("BaoE"));
			br.setInsurancePrice(Item.getJSONObject("HcXiuLiChang").getString("BaoFei"));
			br.setType("0" + Item.getString("HcXiuLiChangType"));
			double pate = bihuTwo.getHcXiuLiChang();
			String pa = new BigDecimal(pate + "").multiply(new BigDecimal("100")).toString();
			br.setPate(pa.substring(0, pa.indexOf('.')));
			insuranceList.add(br);
		}

		if (Item.getJSONObject("ZiRan").getDouble("BaoFei") > 0) {// 自然损失险
			Insurance bnp = new Insurance();
			bnp.setCode(InsuranceCode.BNP.getCode());
			bnp.setAuount(Item.getJSONObject("ZiRan").getString("BaoE"));
			bnp.setInsurancePrice(Item.getJSONObject("ZiRan").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianZiRan").getDouble("BaoFei") > 0) {
				bnp.setIndemnityPrice(Item.getJSONObject("BuJiMianZiRan").getString("BaoFei"));
				bnp.setIndemnity("true");
			} else
				bnp.setIndemnity("false");
			insuranceList.add(bnp);
		}

		if (Item.getJSONObject("SheShui").getDouble("BaoFei") > 0) {// 发动机涉水险
			Insurance bw = new Insurance();
			bw.setCode(InsuranceCode.BW.getCode());
			bw.setAuount(Item.getJSONObject("SheShui").getString("BaoE"));
			bw.setInsurancePrice(Item.getJSONObject("SheShui").getString("BaoFei"));
			if (Item.getJSONObject("BuJiMianSheShui").getDouble("BaoFei") > 0) {
				bw.setIndemnityPrice(Item.getJSONObject("BuJiMianSheShui").getString("BaoFei"));
				bw.setIndemnity("true");
			} else
				bw.setIndemnity("false");
			insuranceList.add(bw);
		}

		if (Item.getJSONObject("HcSanFangTeYue").getDouble("BaoFei") > 0) {// 第三方特约险
			Insurance btt = new Insurance();
			btt.setCode(InsuranceCode.BTT.getCode());
			btt.setAuount(Item.getJSONObject("HcSanFangTeYue").getString("BaoE"));
			btt.setInsurancePrice(Item.getJSONObject("HcSanFangTeYue").getString("BaoFei"));
			btt.setIndemnity("false");
			insuranceList.add(btt);
		}

		result.setCarOrder(order);
		result.setInsuranceList(insuranceList);
	}

	/**
	 * 壁虎1号接口访问处理
	 * 
	 * @throws Exception
	 */
	private void checkOneIter() throws Exception {
		setInterfaceOne();
		JSONObject oneRes = new OneInterface().one(bihuOne);
		if (null == oneRes || oneRes.isEmpty())
			throw new Exception("壁虎接口1返回空");

		if (!"1".equals(oneRes.getString("BusinessStatus").trim()))
			return;
		else {
			String busTime = oneRes.getJSONObject("UserInfo").getString("NextBusinessStartDate");
			if (!StringUtil.isNulls(busTime)) {// 商业险起保时间设定---调1号接口成功情况下，并取到下年起保时间
				if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(busTime).getTime() > new Date().getTime()){
					busBeginTime = busTime;
					if((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(busBeginTime).getTime()-new Date().getTime())/(1000*60*60*24)>90)
						throw new Exception("该车商业险重复投保或到期时间大于90天");
				}
			}

			String traTime = oneRes.getJSONObject("UserInfo").getString("NextForceStartDate");
			if (!StringUtil.isNulls(traTime)) {// 交强险起保时间设定---调1号接口成功情况下，并取到下年起保时间
				if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(traTime).getTime() > new Date().getTime()){
					traBeginTime = traTime;
					if((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(traBeginTime).getTime()-new Date().getTime())/(1000*60*60*24)>90)
						throw new Exception("该车交强险重复投保或到期时间大于90天");
				}
			}
		}
	}
	
	/**
	 * 设置壁虎接口1请求参数信息
	 */
	private void setInterfaceOne() {
		bihuOne.setLicenseNo(param.getLicense().getLicenceNo());// 车牌号
		bihuOne.setCarVin(param.getLicense().getFrameNo());// 车架号
		bihuOne.setEngineNo(param.getLicense().getEngineNo());// 发动机号
		bihuOne.setGroup(1);
		bihuOne.setCustKey(custzkey);
		bihuOne.setTimeFormat(1);
	}

	/**
	 * 设置壁虎接口3请求参数信息
	 */
	private void setThreeBiHu() {
		bihuThree.setLicenseNo(param.getLicense().getLicenceNo());// 车牌号
		bihuThree.setQuoteGroup(group);
		bihuThree.setCustKey(bihuTwo.getCustKey());
		bihuThree.setTimeFormat(1);// 显示时间格式年月日 时分秒
		bihuThree.setShowVehicleInfo(1);// 是否展示车型信息:0 否 1：是
		bihuThree.setRenewalCarType(0);// 大小号牌：0小车，1大车，默认0
	}

	/**
	 * 设置车主以及需报价保险公司信息
	 * 
	 */
	private void setOwer() {
		bihuTwo.setLicenseNo(param.getLicense().getLicenceNo());// 车牌号
		bihuTwo.setCarOwnersName(param.getOwner().getName());// 车主姓名
		bihuTwo.setIdCard(param.getOwner().getIdCard());// 车主身份证
		bihuTwo.setOwnerIdCardType("1");// 证件类型
		String source = EnumUtil.getCompany(company.getCompanyCode()).getSource() + "";
		group = Integer.valueOf(source.substring(0, source.indexOf('.')));
		bihuTwo.setQuoteGroup(group);// 需要报价的 保险资源的枚举值之和
		bihuTwo.setSubmitGroup(0);// 需要核保的 保险资源的枚举值之和
		bihuTwo.setIsPublic(2);// 非公车
	}

	/**
	 * 设置行驶证信息
	 */
	private void setLincense() {
		bihuTwo.setEngineNo(param.getLicense().getEngineNo());
		bihuTwo.setCarVin(param.getLicense().getFrameNo());
		bihuTwo.setRegisterDate(param.getLicense().getRegisteredTime());
		bihuTwo.setMoldName(param.getLicense().getLableNo());
	}

	/**
	 * 设置算费保险信息
	 * 
	 * @throws Exception
	 */
	private void setInsuranceOne() throws Exception {
		boolean bus = (null != insurance(InsuranceCode.BTR));// 第三责任险 都默认true
		boolean tra = (null != insurance(InsuranceCode.TP)); // 交强险
		if (!bus && !tra)
			throw new Exception("必须选择商业险或交强险进行投保");
		if (!bus && tra)
			// 0:单商业 ，1：商业+交强车船，2：单交强+车船
			bihuTwo.setForceTax(2);
		else if (bus && !tra)
			bihuTwo.setForceTax(0);
		else
			bihuTwo.setForceTax(1);

		if (bus) {
			if (null == busBeginTime)
				busBeginTime = TimeBase.getDay("yyyy-MM-dd", CompanyValue.nextDay()) + " 00:00:00";
			long times = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(busBeginTime).getTime() / 1000;
			// 下一次商业险起保时间设置
			bihuTwo.setBizTimeStamp(times + "");
		}

		if (tra) {
			if (null == traBeginTime)
				traBeginTime = TimeBase.getDay("yyyy-MM-dd", CompanyValue.nextDay()) + " 00:00:00";
			long times = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(traBeginTime).getTime() / 1000;
			// 下一次交强险起保时间设置
			bihuTwo.setForceTimeStamp(times + "");
		}

		setOwer();

		setLincense();

		setCar();

		setInsuranceTwo();
	}

	/**
	 * 设置车辆信息
	 */
	private void setCar() {
		String code = company.getModelCode();
		if(code.indexOf('.')>0)
			code = code.substring(0, code.indexOf('.'));
		if (StringUtil.valFormat(code, "^[0-9]{4,}$")) {
			bihuTwo.setPurchasePrice(Double.valueOf(code));// 购置价格
		} else {
			bihuTwo.setAutoMoldCode(code);// 精友编码
		}

		bihuTwo.setRenewalCarType(0);// 大小号牌：0小车，1大车，默认0
		bihuTwo.setQuoteParalelConflict(0);// 报价并发冲突检查标识：0（默认） 1：检测
		bihuTwo.setCustKey(custzkey); // 客户端标识（用来区分客户）（10-32位字符）
	}

	/**
	 * 设置壁虎接口保险列表
	 */
	private void setInsuranceTwo() {
		Insurance bg = insurance(InsuranceCode.BG);// 玻璃险
		if (null == bg)
			// 设置玻璃单独破碎险，0-不投保，1国产，2进口
			bihuTwo.setBoLi(0.0);
		else
			bihuTwo.setBoLi(bg.getType().equals("01") ? 2.0 : 1.0);

		Insurance bl = insurance(InsuranceCode.BL);// 车损险
		if (null == bl) {
			// 机动车损失保险，0-不投保，1-投保
			bihuTwo.setCheSun(0.0);
			// 不计免赔险(车损) ，0-不投保，1投保
			bihuTwo.setBuJiMianCheSun(0.0);
		} else {
			bihuTwo.setCheSun(1.0);
			bihuTwo.setBuJiMianCheSun(bl.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bt = insurance(InsuranceCode.BT);// 盗抢险
		if (null == bt) {
			bihuTwo.setDaoQiang(0.0);
			bihuTwo.setBuJiMianDaoQiang(0.0);
		} else {
			bihuTwo.setDaoQiang(1.0);
			bihuTwo.setBuJiMianDaoQiang(bt.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance btr = insurance(InsuranceCode.BTR);// 三者险
		if (null == btr) {
			bihuTwo.setSanZhe(0.0);
			bihuTwo.setBuJiMianSanZhe(0.0);
		} else {
			bihuTwo.setSanZhe(Double.valueOf(btr.getAuount()));
			bihuTwo.setBuJiMianSanZhe(btr.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bd = insurance(InsuranceCode.BD);// 司机险
		if (null == bd) {
			bihuTwo.setSiJi(0.0);
			bihuTwo.setBuJiMianSiJi(0.0);
		} else {
			bihuTwo.setSiJi(Double.valueOf(bd.getAuount()));
			bihuTwo.setBuJiMianSiJi(bd.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bp = insurance(InsuranceCode.BP);// 乘客险
		if (null == bp) {
			bihuTwo.setChengKe(0.0);
			bihuTwo.setBuJiMianChengKe(0.0);
		} else {
			bihuTwo.setChengKe(Double.valueOf(bp.getAuount()));
			bihuTwo.setBuJiMianChengKe(bp.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bn = insurance(InsuranceCode.BN);// 划痕险
		if (null == bn) {
			bihuTwo.setHuaHen(0.0);
			bihuTwo.setBuJiMianHuaHen(0.0);
		} else {
			bihuTwo.setHuaHen(Double.valueOf(bn.getAuount()));
			bihuTwo.setBuJiMianHuaHen(bn.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bw = insurance(InsuranceCode.BW);// 发动机涉水险
		if (null == bw) {
			bihuTwo.setSheShui(0.0);
			bihuTwo.setBuJiMianSheShui(0.0);
		} else {
			bihuTwo.setSheShui(1.0);
			bihuTwo.setBuJiMianSheShui(bw.getIndemnity().equals("true") ? 1.0 : 0.0);
		}

		Insurance bnp = insurance(InsuranceCode.BNP);// 自然损失险
		if (null == bnp) {
			bihuTwo.setZiRan(0.0);
			bihuTwo.setBuJiMianZiRan(0.0);
		} else {
			bihuTwo.setZiRan(1.0);
			bihuTwo.setBuJiMianZiRan("true".equals(bnp.getIndemnity()) ? 1.0 : 0.0);
		}

		Insurance br = insurance(InsuranceCode.BR);// 指定修理险险
		if (null != br) {
			if (br.getType().equals("01")) {
				bihuTwo.setHcXiuLiChang(0.6);
				bihuTwo.setHcXiuLiChangType(1);
			} else {
				bihuTwo.setHcXiuLiChang(0.3);
				bihuTwo.setHcXiuLiChangType(0);
			}
		}

		bihuTwo.setBuJiMianJingShenSunShi(0.0);// 不计免精神损失0-不投保，1投保

		Insurance btt = insurance(InsuranceCode.BTT);// 指定第三方特约险 0-不投保，1投保

		if ( null != bl) {
			if(null != btt)
				bihuTwo.setHcSanFangTeYue(1.0);
			else
				bihuTwo.setHcSanFangTeYue(0.0);
		}

	}

	/**
	 * 获取请求参数险别
	 * 
	 * @param iCode
	 * @return
	 */
	private Insurance insurance(InsuranceCode iCode) {
		for (Insurance i : param.getInsuranceList()) {
			if (i.getCode().equals(iCode.getCode()))
				return i;
		}

		return null;
	}
}
